export const state = () => ({
  id: '',
  name: '',
  bio: '',
  image: '',
  post: null,
  posts: null,
  allLikes: 0,
})

export const mutations = {
  getProfile(state,res){
    state.id = res.data.user[0].id
    state.name = res.data.user[0].name
    state.bio = res.data.user[0].bio
    state.image = res.data.user[0].image
    state.posts = res.data.posts
  },
  resetUser(state) {
    state.id = ''
    state.name = ''
    state.bio = ''
    state.image = ''
    state.posts = ''
  },
  allLikesCount(state) {
    state.allLikes = 0
    for (let i = 0; i < state.posts.length; i++) {
      const post = state.posts[i];
      for (let j = 0; j < post.likes.length; j++) {
        state.allLikes++
      }
    }
  },
}
export const actions = {
  async getProfile({commit},id) {
    commit('resetUser')
    let res = await this.$axios.get(`/api/profiles/${id}`)
    commit('getProfile',res)
    commit('allLikesCount')
  },
}
